//
//  MessageListViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/9/24.
//

#import "MessageListViewController.h"
#import "MessageModel.h"
#import "MessageListTableViewCell.h"
#import "MessageDetailsViewController.h"

@interface MessageListViewController ()
@property (nonatomic ,strong) NSMutableArray <MessageModel *> * modelArray;

@end

@implementation MessageListViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self refreshingData];
}

- (NSMutableArray<MessageModel *> *)modelArray{
    if(!_modelArray){
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView * topview = [UIView new];
    topview.backgroundColor = KcolorHex(@"#0967FF");
    [self.view addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(Knav_HeightX);
    }];
    MJWeakSelf
    UIView * backview = [UIView new].addClickBlock(^{
        [weakSelf back];
    });
    [topview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.bottom.offset(0);
        make.width.offset(kwt(140));
    }];
    
    UIImageView * leftimg = kimageView(@"white_path");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(20);
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UILabel * leftlab = [DTKTool labelWithText:self.titleStr andFont:kCFont(19) andTextColor:UIColor.whiteColor];
    [backview addSubview:leftlab];
    [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(43));
        make.centerY.offset(0);
    }];
    UIImageView * TJimg = kimageView(@"clean_up");
    TJimg.addClickBlock(^{
        [weakSelf YDApi];
    });
    [topview addSubview:TJimg];
    [TJimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(29);
        make.right.offset(kwt(-15));
        make.centerY.equalTo(backview);
    }];
    
    [self.view addSubview:self.tableview];
    self.tableview.backgroundColor = KcolorHex(@"#ECEDEE");
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.equalTo(topview.mas_bottom);
    }];
    
    [self addFooterView:^{
        [weakSelf addMoreData];
    }];
    [self addHeaderView:^{
        [weakSelf refreshingData];
    }];
    
}

- (void)YDApi{
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:@(self.type) forKey:@"type"];
    [SCNetManger PostsysSmsAppBatchReadWithDict:dict  andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code ==200){
            [self.view showWarning:model.message];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                // 2秒后执行的代码
                [self refreshingData];
            });
        } else {
            [self.view showWarning:model.message];
        }
    }];
    
}

- (void)refreshingData{
    self.pageNo = 1;
    [self.modelArray removeAllObjects];
    [self loadNewData];
}

- (void)loadNewData{
    [self.view showBusyHUD];
    
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:@(self.pageNo) forKey:@"pageNo"];
    [dict setObject:@(self.pageSize) forKey:@"pageSize"];
    [dict setObject:@(self.type) forKey:@"type"];
    [SCNetManger PostsysSmsAppListWithDict: dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if(model.code ==200){
            NSArray <MessageModel *>* array = [NSArray yy_modelArrayWithClass:MessageModel.class json:model.result[@"records"]];
            [self.modelArray addObjectsFromArray:array];
            if(array.count == 0 && self.modelArray.count > 0){
                [self.tableview.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableview.mj_footer endRefreshing];
            }
            [self.tableview.mj_header endRefreshing];
            [self.tableview reloadData];
            
        } else {
            [self.view showWarning:model.message];
        }
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kwt(8);
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  self.modelArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return  self.modelArray[indexPath.row].CellH;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    
    MessageListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MessageListTableViewCell"];
    if (cell == nil) {
        cell = [[MessageListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MessageListTableViewCell"];
    }
    if(self.modelArray.count > indexPath.row){
        cell.model = self.modelArray[indexPath.row];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell ;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MessageDetailsViewController * vc = [MessageDetailsViewController new];
    vc.ID = self.modelArray[indexPath.row].ID;
    [self.navigationController pushViewController:vc animated:YES];
    
}
@end
